package com.ruoyi.common.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.SneakyThrows;

import java.math.BigDecimal;

/**
 * @Program: socializeMicroApi
 * @ClassName: BigDecimalSerializer
 * @author: zhouzihao
 * @date: 2023年10月23日, 0023 下午 04:42:39
 * @version: 1.0.0
 * @Description:
 * @Time： 2023-10-23 16:42
 */
public class BigDecimalSerializer extends StdSerializer<BigDecimal> {
    public BigDecimalSerializer() {
        super(BigDecimal.class);
    }
	@SneakyThrows
	@Override
	public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
		if (null != bigDecimal ) {
			String plainString = bigDecimal.stripTrailingZeros().toPlainString();
			jsonGenerator.writeString(plainString);
		} else {
			jsonGenerator.writeString(BigDecimal.ZERO.toPlainString());
		}
	}
}
